# 印尼语重读音节调整，调整在元音的后面
import re,os,sys
def read_file_line(filename):
   with open(filename,'r',encoding='utf-8') as r:
       lines=r.readlines()
   return lines
def mkdir(path):
    if not os.path.exists(path):
        os.makedirs(path)
def get_newline(rt,file,out_path):
    print(file)
    mkdir(out_path)
    filename=os.path.join(rt,file)
    # filename='葡语词典【43197】.txt'
    new_filename=os.path.join(out_path,file)
    other_name=os.path.join(out_path,file.replace('.txt','_未修改.txt'))
    lines=read_file_line(filename)
    # vowel1=['aː', 'eː', 'iː','oː','uː','ɛː','øː','yː','aʊ','ɔʏ','aɪ',
    # 'a', 'ɛ', 'ɪ', 'o', 'u', 'œ','y','ə','ɐ']  # 单元音
    vowel1=[v.strip() for v in read_file_line('勿删/元音.txt')]
    erro_list=[]
    new_line=[] #记录修改后的音节
    # lines=['pincéis	p ĩ . s ɛj ʃ1']
    without_find={}
    for i in range(0,len(lines),2):
        line=lines[i+1]
        line=re.sub(' +',' ',line)
        line=line.replace('1.','1 .')
        new_syllable=[]
        word=lines[i]
        syllable=line.strip()
        flag1=re.findall('1',syllable) #在整个音节中查找1，如果找到则执行下一步
        if flag1!=[]:
            syllable = syllable+ ' .'
            ele_list = re.findall('.*?\.', syllable)
            new_ele_list=[]#记录新的音节组成
            for e in range(len(ele_list)):
                new_alp_list=[] #记录每个字母的发音
                find_one=re.findall('1',ele_list[e])
                true_flag=0
                erro_flag=0
                if find_one!=[]:
                    alp_list=ele_list[e].strip().split(' ')
                    # print(alp_list)
                    for a in range(len(alp_list)):
                        if true_flag==0:
                            # print(alp_list[a])
                            if alp_list[a][-1]=='1':
                                if alp_list[a][:-1] in vowel1:
                                    true_flag=1
                    # 如果本来就是元音后面加重音标记符，就直接将原来的数据添加回去
                    if true_flag==1:
                        new_alp_list.append(ele_list[e])
                    # 如果辅音发音后面跟1
                    else:
                        # print('22')
                        alp_one_flag=-100 #找到有1的字符串，并记录
                        replace_flag=-100
                        for a in range(len(alp_list)):
                            a1 = len(alp_list) - 1 - a  # 倒着遍历
                            if replace_flag==-100:
                                if alp_list[a1][-1]=='1':
                                    # print(alp_list[a1])
                                    alp_one_flag=a1
                                if a1<alp_one_flag and alp_list[a1] in vowel1:
                                    replace_flag=a1 #找到前面有元音发音
                        # print(alp_one_flag)
                        # print(replace_flag)
                        if replace_flag==-100: #没找到
                            new_ele_list.append(ele_list[e])
                            if line not in without_find:
                                without_find[line]=0
                        else: #找到了
                            for a in range(len(alp_list)):
                                if a==alp_one_flag:
                                    new_alp_list.append(alp_list[a][:-1])
                                    continue
                                if a==replace_flag:
                                    new_alp_list.append(alp_list[a]+'1')
                                    continue
                                else:
                                    new_alp_list.append(alp_list[a])
                                    continue
                    new_ele_list.append(' '.join(new_alp_list))
                else:
                    new_ele_list.append(ele_list[e])
            new_syllable.append(''.join(new_ele_list)[:-2])
        else: #如果没找到1，则直接添加到新行中
            new_syllable.append(syllable)
        new_line.append(word+'\t'+''.join(new_syllable)+'\n')
    line_str=''.join(new_line)
    line_str=re.sub(' +',' ',line_str)
    # print(line_str)
    with open(new_filename,'w',encoding='utf-8') as w:
        w.write(line_str)
    # without_data=list(without_find.keys())
    # if without_data!=[]:
    #     with open(other_name, 'w', encoding='utf-8') as w:
    #         w.write(''.join(without_data))
if len(sys.argv)<3:
        print("usage: python adjustment.py in_path out_path")
        exit()
else:
    path = sys.argv[1]
    output = sys.argv[2]
    for rt,_,files in os.walk(path):
        for file in files:
            get_newline(rt,file,output)

